package com.fortune.controller;

import com.faneui.common.exception.BusinessException;
import com.fortune.dal.model.Cust;
import com.fortune.dal.model.Debt;
import com.fortune.service.CustService;
import com.fortune.service.DebtService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("debt")
public class DebtController  extends AbstractController{

    @Autowired
    private DebtService debtService ;

    @Autowired
    private CustService custService ;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws BusinessException {
        ModelAndView mv = new ModelAndView("debt");
        mv.addObject("menuFlag", "debt");
        return mv;
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Debt> list(@ModelAttribute Debt debt) throws BusinessException {
        List<Debt> debts = Lists.newArrayList() ;
        try {
            debts = debtService.queryDebtList(debt) ;
            for(Debt d:debts){
                Cust cust = custService.queryCust(d.getCustId()) ;
                if(cust!= null) {
                    d.setCustName(cust.getName());
                }
            }
        }catch (Exception ex) {
            logger.error("",ex);
            return Lists.newArrayList() ;
        }
        return debts;
    }

}
